package com.rd.student.jike.class2.xuezhixin.day0728;

import java.io.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class Work04 {
    public static void main(String[] args) throws Exception{
        File file = new File("all.txt");
        InputStream is = new FileInputStream(file);
        byte[] arr = new byte[1024];
        is.read(arr);
        String str = new String(arr);
        String str1 = "";
        // 将学生名字存到链表 list 中
        LinkedList list = new LinkedList();
        for (int i = 0;i<str.length();i++){
            if(str.charAt(i) == '、') {
                list.add(str1);
                str1 = "";
                continue;
            }
            str1 = str1 + "" + str.charAt(i);
        }

        // 读取random.txt文件中的名字
        File file1 = new File("random.txt");
        InputStream is1 = new FileInputStream(file1);
        byte[] arrRandom = new byte[1024];
        int len = is1.read(arrRandom);
        if(len == -1){
            System.out.println("random.txt文件是空文件，请存放数据");
            return;
        }
        String strRandom = new String(arrRandom,0,len);
        String strRandom1 = "";
        // 将ranndom.txt文件中的名字存在链表list1中
        LinkedList list1 = new LinkedList();
        for(int i = 0;i<strRandom.length();i++){
            if(strRandom.charAt(i) == ',') {
                list1.add(strRandom1);
                strRandom1 = "";
                continue;
            }
            strRandom1= strRandom1 + "" + strRandom.charAt(i);
        }

        // 从list中删除list1相同的名字.并存到list2上
        LinkedList list2 = new LinkedList();
        for(int i = 0;i<list.size();i++){
            String strList = (String) list.get(i);
            for(int j = 0;j<list1.size();j++){
                String strList1 = (String) list1.get(j);
                if(strList.equals(strList1)){
                   break;
                }
                if(j == list1.size() -1){
                    list2.add(strList);
                }
            }
        }


        // 开始随机名字
        Collections.shuffle(list2);
        OutputStream os = new FileOutputStream("random.txt");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您想随机名字的个数：");
        int num = scanner.nextInt();
        String str2 = strRandom;
        System.out.println("随机到的名字：");
        for(int i = 0;i<num;i++){

            System.out.println(list2.get(i));
            str2 = list2.get(i) + "," + str2;
        }
        byte[] arr2 = str2.getBytes();

        os.write(arr2);

//        // 没随机到的
//        LinkedList list1 = new LinkedList();
//        for (int i = num;i<list.size();i++){
//            list1.add(list.get(i));
//        }
//        Collections.shuffle(list1);
//        String str3 = "";
//        System.out.println("第二波随机到的：");
//        for (int i = 0;i<num;i++){
//
//            System.out.println(list1.get(i));
//            str3 = str3 + "," + list1.get(i);
//        }
//        byte[] arr3 = str3.getBytes();
//        os.write(arr3);
//

        is1.close();
        os.close();
        is.close();
    }
}
